package com.feidee.fdhadoop.kerberos;

import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdcommon.constant.CommonConstant;
import org.apache.log4j.Logger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.InputStream;
import java.util.Properties;

public class KerberosListener implements ServletContextListener {
	private static Logger logger = Logger.getLogger(KerberosListener.class);

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		InputStream in = null;
		try {
			in = KerberosAuthentication.class.getClassLoader().getResourceAsStream("disconf.properties");
			Properties properties = new Properties();
			properties.load(in);
			String tempEnv = properties.getProperty("disconf.env");
			if (!"test".equals(tempEnv) && !"dev".equals(tempEnv) && !"autotest".equals(tempEnv)) {
				CustomConfiguration.setString(CommonConstant.ENV_KEY, CommonConstant.ENV_LIVE);
			} else {
				CustomConfiguration.setString(CommonConstant.ENV_KEY, CommonConstant.ENV_TEST);
			}
		} catch (Exception e) {
			logger.error("Kerberos 监听获取环境异常", e);
		} finally {
			try {
				in.close();
			} catch (Exception e) {
				logger.error("Kerberos 监听关闭文件流异常", e);
			}
		}
		KerberosAuthentication.authenticate();
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent) {

	}
}
